


package com.hehe.Common;

import com.alibaba.fastjson2.reader.ObjectReader6;
import com.hehe.POJO.Orders;
import com.hehe.Repository.OrdersRepository;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.swing.plaf.BorderUIResource;
import java.util.Map;

@Controller
public class PayCallback {

    @Resource
    private OrdersRepository ordersRepository;

    @RequestMapping("/api/pay/paycallback")
    @ResponseBody
    public String abc(HttpServletRequest request) {
        System.out.println("=================================  api/pay/paycallback  ==============================================");
        // 记得 map 第二个泛型是数组 要取 第一个元素 即[0]
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println("展示 回调的所有结果：");
        // 处理 回调 结果
        parameterMap.keySet().stream().forEach((k) -> {
            System.out.println(k + ":" + parameterMap.get(k)[0]);
            if ("plugins".equals(k)) {
                String username = k.split("===")[0];
                String id = k.split("===")[1];
                String price = k.split("===")[2];

                Orders orders = new Orders();
                orders.setUsername(username);
                orders.setId(Long.parseLong(id));
                orders.setOrderPrice(price);

                ordersRepository.save(orders);
            }
        });

        System.out.println("展示 回调的所有结果完成");
        System.out.print("\n最终结果是:");

        if ("OD".equals(parameterMap.get("status")[0])) {
            System.out.println("用户支付成功了");
        } else {
            System.out.println("用户支付不成功");
        }
        return "ok";
    }
}





/*



展示 回调的所有结果：
trade_order_id:4xhczeq69nt2jphbg4qgxq3cv60agn9n
total_fee:0.01
transaction_id:2024082622001467331432047006
open_order_id:20263180528
order_title:摄影
status:OD
plugins:我是备注信息
nonce_str:6417361320
time:1724631630
appid:201906167140
hash:60e13a34664f8c63e0aea55d8c0709fc
展示 回调的所有结果完成

最终结果是:用户支付成功了



*/

